#!/bin/bash
# **********************************************************
# * Author : liangliangSu
# * Email : sll917@hotmail.com
# * Create time : 2022-10-20 11:10
# * Filename : 14awk_用法例子.sh
# **********************************************************
#应用1
echo '1)------------------------------完美分割线------------------------'
awk -F: '{print NF}' /etc/passwd | awk 'NR<=10 {print}'        #输出文件每行有多少字段
echo '2)------------------------------完美分割线------------------------'
awk -F: '{print $1,$2,$3,$4,$5}' /etc/passwd | awk 'NR<=10 {print}'       #输出前5个字段
echo '3)------------------------------完美分割线------------------------'
awk -F: '{print $1,$2,$3,$4,$5}' OFS='\t' /etc/passwd | awk 'NR<=10 {print}'   
#输出前5个字段并使用制表符分隔输出
echo '4)------------------------------完美分割线------------------------'
awk -F: '{print NR,$1,$2,$3,$4,$5}' OFS='\t' /etc/passwd | awk 'NR<=10 {print}'  
#制表符分隔输出前5个字段，并打印行号
  
#应用2
echo '5)------------------------------完美分割线------------------------'
awk -F'[:#]' '{print NF}'  /etc/passwd | awk 'NR<=10 {print}'   #指定多个分隔符: #，输出每行多少字段
echo '6)------------------------------完美分割线------------------------'
awk -F'[:#]' '{print $1,$2,$3,$4,$5,$6,$7}' OFS='\t' /etc/passwd | awk 'NR<=10 {print}'  
#制表符分隔输出多字段
  
#应用3
echo '7)------------------------------完美分割线------------------------'
awk -F'[:#/]' '{print NF}' /etc/passwd | awk 'NR<=10 {print}'    #指定三个分隔符，并输出每行字段数
echo '8)------------------------------完美分割线------------------------'
awk -F'[:#/]' '{print NR,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' /etc/passwd | awk 'NR<=10 {print}'  
#制表符分隔输出多字段
  
#应用4
#计算/home目录下，普通文件的大小，使用KB作为单位
echo '9)------------------------------完美分割线------------------------'
ls -l|awk 'BEGIN{sum=0} !/^d/{sum+=$5} END{print "total size is:",sum/1024,"KB"}'
ls -l|awk 'BEGIN{sum=0} !/^d/{sum+=$5} END{print "total size is:",int(sum/1024),"KB"}'    
#int是取整的意思
  
#应用5
#统计netstat -anp 状态为LISTEN和CONNECT的连接数量分别是多少
echo '10)------------------------------完美分割线------------------------'
sudo netstat -anp|awk '$6~/LISTEN|CONNECTED/{sum[$6]++} END{for (i in sum) printf "%-10s %-6s %-3s \n", i," ",sum[i]}'
  
#应用6
#统计/home目录下不同用户的普通文件的总数是多少？
echo '11)------------------------------完美分割线------------------------'
ls -l|awk 'NR!=1 && !/^d/{sum[$3]++} END{for (i in sum) printf "%-6s %-5s %-3s \n",i," ",sum[i]}'  
#mysql        199
#root           374

#应用7
#统计/home目录下不同用户的普通文件的大小总size是多少？
echo '12)------------------------------完美分割线------------------------'
ls -l|awk 'NR!=1 && !/^d/{sum[$3]+=$5} END{for (i in sum) printf "%-6s %-5s %-3s %-2s \n",i," ",sum[i]/1024/1024,"MB"}'
  
#应用8
#输出成绩表
echo '13)------------------------------完美分割线------------------------'
awk 'BEGIN{math=0;eng=0;com=0;printf "Lineno.   Name    No.    Math   English   Computer    Total\n";printf "------------------------------------------------------------\n"}{math+=$3; eng+=$4; com+=$5;printf "%-8s %-7s %-7s %-7s %-9s %-10s %-7s \n",NR,$1,$2,$3,$4,$5,$3+$4+$5} END{printf "------------------------------------------------------------\n";printf "%-24s %-7s %-9s %-20s \n","Total:",math,eng,com;printf "%-24s %-7s %-9s %-20s \n","Avg:",math/NR,eng/NR,com/NR}' test2.txt

#应用9
#计算文件大小：
echo '14)------------------------------完美分割线------------------------'
ls -l *.txt | awk '{sum+=$6} END {print sum}'

#应用10
#打印九九乘法表：
echo '15)------------------------------完美分割线------------------------'
seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}'
